﻿// Type: D2L.Extensibility.AuthSdk.Impl.D2LAppContext
// Assembly: D2L.Extensibility.AuthSdk, Version=1.1.0.0, Culture=neutral, PublicKeyToken=null
// MVID: B2FBA42B-591C-474F-A782-6FF215FEDFB9
// Assembly location: C:\Users\leandro.perdizo\Desktop\D2L.Extensibility.AuthSdk.dll

using D2L.Extensibility.AuthSdk;
using System;
using System.Collections.Specialized;
using System.Web;

namespace D2L.Extensibility.AuthSdk.Impl
{
  internal sealed class D2LAppContext : ID2LAppContext
  {
    private const string APP_ID_PARAMETER = "x_a";
    private const string APP_KEY_PARAMETER = "x_b";
    private const string CALLBACK_URL_PARAMETER = "x_target";
    private const string USER_ID_CALLBACK_PARAMETER = "x_a";
    private const string USER_KEY_CALLBACK_PARAMETER = "x_b";
    private const string AUTHENTICATION_SERVICE_URI_PATH = "/d2l/auth/api/token";
    private readonly ITimestampProvider m_timestampProvider;
    private readonly string m_appId;
    private readonly string m_appKey;

    internal D2LAppContext(string appId, string appKey, ITimestampProvider timestampProvider)
    {
      this.m_appId = appId;
      this.m_appKey = appKey;
      this.m_timestampProvider = timestampProvider;
    }

    Uri ID2LAppContext.CreateUrlForAuthentication(HostSpec authenticatingHost, Uri landingUri)
    {
      UriBuilder uriBuilder = authenticatingHost.ToUriBuilder();
      uriBuilder.Path = "/d2l/auth/api/token";
      uriBuilder.Query = this.BuildAuthenticationUriQueryString(landingUri);
      return uriBuilder.Uri;
    }

    ID2LUserContext ID2LAppContext.CreateUserContext(Uri authenticationCallbackUri, HostSpec apiHost)
    {
      NameValueCollection nameValueCollection = HttpUtility.ParseQueryString(authenticationCallbackUri.Query);
      string userId = nameValueCollection["x_a"];
      string userKey = nameValueCollection["x_b"];
      if (userId == null || userKey == null)
        return (ID2LUserContext) null;
      else
        return (ID2LUserContext) new D2LUserContext(this.m_timestampProvider, this.m_appId, this.m_appKey, userId, userKey, apiHost);
    }

    ID2LUserContext ID2LAppContext.CreateUserContext(string userId, string userKey, HostSpec apiHost)
    {
      return (ID2LUserContext) new D2LUserContext(this.m_timestampProvider, this.m_appId, this.m_appKey, userId, userKey, apiHost);
    }

    ID2LUserContext ID2LAppContext.CreateUserContext(UserContextProperties savedProps)
    {
      HostSpec apiHost = new HostSpec(savedProps.Scheme, savedProps.HostName, savedProps.Port);
      return (ID2LUserContext) new D2LUserContext(this.m_timestampProvider, this.m_appId, this.m_appKey, savedProps.UserId, savedProps.UserKey, apiHost);
    }

    ID2LUserContext ID2LAppContext.CreateAnonymousUserContext(HostSpec apiHost)
    {
      return (ID2LUserContext) new D2LUserContext(this.m_timestampProvider, this.m_appId, this.m_appKey, (string) null, (string) null, apiHost);
    }

    private string BuildAuthenticationUriQueryString(Uri callbackUri)
    {
      string absoluteUri = callbackUri.AbsoluteUri;
      return "x_a=" + this.m_appId + "&x_b=" + D2LSigner.GetBase64HashString(this.m_appKey, absoluteUri) + "&x_target=" + HttpUtility.UrlEncode(absoluteUri);
    }
  }
}
